﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bzzb.Model;
using FreeSql.Internal.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Bzzb.Controllers
{
    public class BaoZhangCheLiangTypeController : BaseController
    {
        private readonly ILogger<BaoZhangCheLiangTypeController> _logger;
        public BaoZhangCheLiangTypeController(ILogger<BaoZhangCheLiangTypeController> logger, IFreeSql fsql) : base(fsql)
        {
            this._logger = logger;
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Index()
        {
            List<BaoZhangCheLiangType> baozhangcheliangtypes = await _fsql.Select<BaoZhangCheLiangType>().Count(out var total).ToListAsync();
            _logger.LogInformation(_fsql.Select<BaoZhangCheLiangType>().ToSql());
            JObject data = new JObject();
            data.Add("total", total);
            data.Add("list", JArray.FromObject(baozhangcheliangtypes));
            return RenderJson(data: data);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Save([FromForm] BaoZhangCheLiangType baozhangcheliangtype)
        {
            long id = await _fsql.Insert(baozhangcheliangtype).ExecuteIdentityAsync();
            baozhangcheliangtype.Id = id;

            return RenderJson(data: baozhangcheliangtype);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Delete([FromForm] long id) {
            int affrows = await _fsql.Delete<BaoZhangCheLiangType>().Where(bzclt => bzclt.Id == id).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Update([FromForm] BaoZhangCheLiangType baozhangcheliangtype) {
            int affrows = await _fsql.Update<BaoZhangCheLiangType>(baozhangcheliangtype.Id).Set(bzclt => bzclt.Fieldinfo, baozhangcheliangtype.Fieldinfo).ExecuteAffrowsAsync();
            BaoZhangCheLiangType data = await _fsql.Select<BaoZhangCheLiangType>().Where(bzclt => bzclt.Id == baozhangcheliangtype.Id).ToOneAsync();
            return RenderJson(data: data);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Show([FromForm] long id) {
            BaoZhangCheLiangType baozhangcheliangtype = await _fsql.Select<BaoZhangCheLiangType>().Where(bzclt => bzclt.Id == id).ToOneAsync();
            return RenderJson(data: baozhangcheliangtype);
        }
    }
}
